import lodash from "lodash";
import $ from "lodash";
import _ from "lodash";

// lodash, $ and _ are same thing
console.log(lodash === $, $ === _);

interface item2 {
  a: number;
  b: number;
}
interface item3 extends item2 {
  c: number;
}

const list: (item2 | item3 | item2[])[] = [
  { a: 1, b: 2, c: 3 },
  { a: 5, b: 8, c: 7 },
  { a: 5, b: 2, c: 3 },
  { a: 5, b: 2 },
  [
    { a: 5, b: 2 },
    { a: 5, b: 9 },
  ],
];
const o = { data: { list } };

console.log("o:", typeof o, o);
console.log("o.data.list", typeof o.data.list, o.data.list);

// 单独调用
if (lodash.hasIn(o, "data.list") && Array.isArray(o.data.list)) {
  const filtered = lodash.filter(o.data.list, (c) => !Array.isArray(c) && "c" in c);
  const sorted = lodash.sortBy(filtered, ["b", "a", "c"]);
  console.log("o.data.list sorted by b, a, c:", sorted);
}

// lodash 链，立即求值
console.log(
  "uniq object in o.data.list sorted by a, b:",
  _.chain(o.data.list)
    .flatten()
    .uniq()
    .sortBy(["a", "b"])
    .uniqBy((c) => [c.a, c.b, c.c || 0].join(""))
    .value()
);

// lodash 链，惰性求值
const chain = $.chain(o.data.list);
// 预先构造好链
const chain2 = chain
  .filter((c) => !Array.isArray(c))
  .map((c) => Object.values(c))
  .flatten()
  .sort()
  .reverse();
// 使用时求值
console.log("max value of o.data.list except objects in array:", chain2.value()[0]);

export {};
